<?php
/**
 * 报价单
 */
quanli_require('module/base/bill/billCost.class.php');
quanli_require('module/base/company/companyMaster.class.php');

class pa_billCost {

    public function execute()
    {
        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0)
        {
            switch($_REQUEST['operFlg'])
            {
                case 1: //订单列表
                    $this->searchCostList();
                    break;

                case 2: //保存报价单
                    $this->saveCost();
                    break;

                default:
                    $this->toError('参数错误');
                    break;
            }
        } else {
            $this->toError('参数错误');
        }
    }

    /**
     * 转跳到错误页
     * @param string $msg 错误信息
     */
    private function toError( $msg )
    {
        global $G_PGLOB;
        $G_PGLOB->display_error_page( $msg );
    }

    /**
     * 报价单列表
     */
    private function searchCostList()
    {
        global $G_PGLOB;
        $G_PGLOB->smarty->display('bill/billCost.tpl');
    }

    /**
     * 保存报价单
     */
    private function saveCost()
    {
        global $G_PGLOB;

        $params = array('useFlg'=>1);
        //分类
        quanli_include('module/base/goods/goodsCategory.class.php');
        $categoryHandler = new goodsCategory( $G_PGLOB->DBCA );
        $category = $categoryHandler->searchBigCategory( $params );
        $G_PGLOB->smarty->assign('category', json_encode($category));

        //物流
        quanli_require('module/base/system/sysSend.class.php');
        $sysSend = new sysSend();
        $send_array = $sysSend->searchSendType( $params );
        $G_PGLOB->smarty->assign('send_array', $send_array);

        $costInfo = null;
        $currDate = date('Y-m-d');
        $validDate = date('Y-m-d', strtotime("+90 day"));

        if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])){
            $billCost = new billCost( $G_PGLOB->DBCA );
            $param = array( 'costCode' => $_REQUEST['code'] );
            $result = $billCost->searchBillCost($param);
            if ($result) {
                $costInfo = $result[0];
                $currDate = date('Y-m-d', strtotime($costInfo->createTime));
                $validDate = date('Y-m-d', strtotime($costInfo->validDate));
            }
            $costInfo->dtl = $billCost->searchCostDtl($_REQUEST['code']);
        }

        //默认客户信息
        $companyInfo = null;
        if(!isset($_REQUEST['code']) && isset($_REQUEST['companyId'])){
            $companyId = $_REQUEST['companyId'];
            //实例化大客户信息类
            $companyMaster = new companyMaster();
            $param = array('company_id'=>$companyId);
            $companyRes = $companyMaster->search( $param );
            if($companyRes) $companyInfo = $companyRes[0];
        }

        $G_PGLOB->smarty->assign('companyInfo', $companyInfo);
        $G_PGLOB->smarty->assign('costInfo', $costInfo);
        $G_PGLOB->smarty->assign('currDate', $currDate);
        $G_PGLOB->smarty->assign('validDate', $validDate);
        $G_PGLOB->smarty->display('bill/billCostAdd.tpl');
    }

}